package com.shuwa.service.impl;


import com.shuwa.dao.UserDao;
import com.shuwa.pojo.Users;
import com.shuwa.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@Service("UserService")
public class UserImpl implements UserService {

   @Resource
   private UserDao userDao;

    @Override
    public Users logCheck(String name)      {
        return userDao.logCheck(name);
    }

    @Override
    public void register(Users user) {
        userDao.Register(user);
    }

    @Override
    public boolean checkNameExist(String username) {
        int num=userDao.checkNameExist(username);
        if(num!=0){
            return true;
        }else{
        return false;
        }
    }

    @Override
    public boolean checkEmailExist(String email) {
        int num=userDao.checkEmailExist(email);
        System.out.println(num);
        if(num!=0){
            return true;
        }else{
            return false;
        }
    }
/*
* 邮箱格式验证
* */
    @Override
    public boolean checkEmailFormat(String email) {
        boolean flag = false;
        try{
            String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
            Pattern regex = Pattern.compile(check);
            Matcher matcher = regex.matcher(email);
            flag = matcher.matches();
        }catch(Exception e){
            flag = false;
        }
        return flag;
    }
}
